var LoginModel = function(){
	var thisLoginModel = this;
	
	this.load = function(){
		RestService.send({
			type: "GET",
			url: '/rest/environments',
			onSuccess: function(data) {
				thisLoginModel.onLoad(data);
			}
		});
	};
	this.sendLogin = function(username, password, environment){
		RestService.send({
			type:'POST',
			url: '/rest/login',
			data: {'username' : username, 'password' : password, 'environment' : environment},
			onSuccess: function(data){
				thisLoginModel.onLogin(data);
			},
			onError: function(status, response) {
				var loginError = {'status': status, 'response': response};
				thisLoginModel.onLoginError(loginError);
			}
		});
	};
	
	this.onLoad = new Subscribable(function(environments){
		return environments;
	});
	this.onLogin = new Subscribable(function(user) {
		return user;
	});
	this.onLoginError = new Subscribable(function(loginError) {
		return loginError;
	});
	
};
//---------------------------------------------------------------------------------------------
var RequestModel = function(){
	var thisModel = this;
	
	this.loadApiVersion = function(){
		RestService.send({
			type: "GET",
			url: '/rest/services/version',
			onSuccess: function(data) {
				thisModel._apiVersion = data;
				thisModel.onLoadApiVersion();
			}
		});
	};
	
	this.load = function(){
		RestService.send({
			type: "GET",
			url: '/rest/services',
			onSuccess: function(data) {
				thisModel._services = data;
				thisModel.onLoad();
			}
		});
	};
	
	this.onLoad = new Subscribable(function(){
		return thisModel._services;
	});
	
	this.loadService = function(api, service){
		RestService.send({
			type: "GET",
			url: '/rest/services/service',
			data: {'api': api, 'service': service},
			onSuccess: function(data) {
				thisModel._service = data;
				thisModel.onLoadService();
			}
		});
	};
	
	this.loadEnums = function(){
		RestService.send({
			type: "GET",
			url: '/rest/services/enums',
			onSuccess: function(data) {
				thisModel._enums = data;
				thisModel.onLoadEnums();
			}
		});
	};
	
	this.onLoadService = new Subscribable(function(){
		return thisModel._service;
	});
	
	this.onLoadEnums = new Subscribable(function(){
		return thisModel._enums;
	});
	
	this.onLoadApiVersion = new Subscribable(function(){
		return thisModel._apiVersion;
	}); 
};
//--------------------------------------------------------------------------------
var ResponseModel = function(){
	var thisModel = this;
	
	this.sendRequest = function(environment, service, user, parameters){
		RestService.send({
			type: "POST",
			url: '/rest/request',
			data: {'environment': environment, 'service': service, 'user': user, 'values': parameters},
			onSuccess: function(data) {
				thisModel._response = data;
				thisModel.onResponse();
			}
		});
	};
	
	this.onResponse = new Subscribable(function(){
		return thisModel._response;
	});
};